/*
* $Id: GLWin.cpp 153 2010-09-26 17:33:07Z volok-aleksej@yandex.ru $
*
* Copyright 2010 by Alexey Volokitin
* All rights reserved
*
*/
#include "Common/include.h"
#include "GLWin.h"
#include "Components/Journal/QueenBeeManager.h"
#include "Visual/UserMessage.h"

#pragma warning(disable: 4407)

CGLWin::CGLWin()
: m_streeDrawer(dynamic_cast<IWnd*>(this)){}
CGLWin::~CGLWin(){
	m_streeDrawer.Clear();
}

void CGLWin::WindowCreate(CWnd* pParent){
	Create(AfxRegisterWndClass(CS_HREDRAW| CS_VREDRAW, LoadCursor(NULL, IDC_ARROW)),
		_T(""), WS_CHILD | WS_VISIBLE | WS_BORDER | WS_VSCROLL, RECT_LEFT_WIN, pParent, LEFT_WIN_ID);
	ScrollUpdate(0);
}

void CGLWin::WindowDestroy(){
	DestroyWindow();
}

void CGLWin::SetQueenBeeId(int nId){
	__super::SetQueenBeeId(nId);
	m_streeDrawer.Clear();
	int nSizeY = 0;
	if(m_nId){
		CQueenBeeManager *pManager = IJournalComponent::GetQueenBeeManager();
		CQueenBeeManager::queen_bee qb = pManager->GetQueenBee(m_nId);
		m_streeDrawer.InsertItem(m_nId, qb.s_strName);
		if(qb.s_nIdParentQueenBee){
			CQueenBeeManager::queen_bee qbParent = pManager->GetQueenBee(qb.s_nIdParentQueenBee);
			m_streeDrawer.InsertParentItem(qb.s_nIdParentQueenBee, qbParent.s_strName);
		}
		std::map<int, CQueenBeeManager::queen_bee> aQb = pManager->GetChildQB(m_nId);
		for(std::map<int, CQueenBeeManager::queen_bee>::iterator it = aQb.begin(); it != aQb.end(); it++)
			m_streeDrawer.AddChildItem(it->first, it->second.s_strName);
		CRect rect;
		GetClientRect(rect);
		nSizeY = m_streeDrawer.CalcView(rect);
	}
	ScrollUpdate(nSizeY);
	Invalidate();
}

void CGLWin::Save(){}
void CGLWin::SetSelectionData(unsigned long data){
	m_selData = HIWORD(data);
}

void CGLWin::ScrollUpdate(int sizeY){
	CRect rect;
	GetClientRect(rect);
	SCROLLINFO si = {0};
	si.cbSize = sizeof(SCROLLINFO);
	si.fMask = SIF_ALL;
	si.nMax = sizeY;
	si.nPage = rect.Size().cy;
	SetScrollInfo(SB_VERT, &si);
}

BEGIN_MESSAGE_MAP(CGLWin, CWnd)
	ON_WM_PAINT()
	ON_WM_VSCROLL()
	ON_WM_LBUTTONDOWN()
	ON_MESSAGE(UM_BUTTONCLICK, OnButtonClick)
END_MESSAGE_MAP()

void CGLWin::OnPaint(){
	CPaintDC dc(this);
	CRect rect;
	GetClientRect(rect);
	PaintInfo(&dc, rect);
}

void CGLWin::PaintInfo(CDC* dc, CRect rect){
	CRect rect1 = rect;
	if(!m_nId){
		CBrush brush;
		brush.CreateSolidBrush(RGB(255,255,255));
		dc->FillRect(rect, &brush);
		brush.DeleteObject();
		dc->SetBkMode(TRANSPARENT);
		CFont* pFont = CCommonFunction::GetFont(_T("Arial"), 20, TRUE, FALSE, FALSE);
		HGDIOBJ oldFont = dc->SelectObject(pFont->m_hObject);
		dc->DrawText(ILanguageManager::GetLanguageManager()->GetString(CHOOSE_QUEEN_BEE_ID).c_str(),
			rect1, DT_WORDBREAK | DT_CALCRECT);
		rect1.MoveToXY(rect.CenterPoint().x - rect1.Width()/2, rect.CenterPoint().y - rect1.Height()/2);
		dc->DrawText(ILanguageManager::GetLanguageManager()->GetString(CHOOSE_QUEEN_BEE_ID).c_str(),
			rect1, DT_CENTER | DT_WORDBREAK);
		dc->SelectObject(oldFont);
		pFont->DeleteObject();
		delete pFont;
	}else
		m_streeDrawer.Paint(dc, rect);
}

LRESULT CGLWin::OnButtonClick(WPARAM wParam, LPARAM lParam){
	SendNotifySel((int)wParam);
	return 0;
}

void CGLWin::SendNotifySel(int nId){
	GLNMHDR *nmhdr = new GLNMHDR;
	nmhdr->idFrom = LEFT_WIN_ID;
	nmhdr->hwndFrom = m_hWnd;
	nmhdr->code = GL_CHANGE_QB;
	nmhdr->m_nParameter = nId;
	GetParent()->SendMessage(WM_NOTIFY, LEFT_WIN_ID, (LPARAM)nmhdr);
	delete nmhdr;
}

void CGLWin::OnLButtonDown(UINT nFlags, CPoint point){
	if(m_nId && m_streeDrawer.ProcessMouse(WM_LBUTTONDOWN, point, 0)){
		m_selData = m_streeDrawer.GetSelItem()->GetId();
		Invalidate();
	}
	__super::OnLButtonDown(nFlags, point);
}

void CGLWin::OnVScroll(UINT nSBCode, UINT nPos, CScrollBar* pScrollBar){
	int newPos = 0;
	if(nSBCode == SB_THUMBPOSITION){
		SetScrollPos(SB_VERT, nPos);
		newPos = nPos;
		m_streeDrawer.SetPos(-newPos);
	}else if(nSBCode == SB_LINEDOWN){
		int nPosOld = GetScrollPos(SB_VERT);
		newPos = nPosOld + 5;
		SetScrollPos(SB_VERT, newPos);
		m_streeDrawer.SetPos(-newPos);
	}
	if(nSBCode == SB_LINEUP){
		int nPosOld = GetScrollPos(SB_VERT);
		newPos = nPosOld - 5;
		SetScrollPos(SB_VERT, newPos);
		m_streeDrawer.SetPos(-newPos);
	}
	Invalidate(FALSE);
}
